<?php
namespace app\api\controller\v1;
class Address extends Base{

    public function __construct() {
        parent::__construct();
        $this->checkLogin();
    }

    //用户地址
    public function lists(){
        //收货地址
        $addrlist = $this->db->query('select * from user_address where user_id=? order by id desc',[$this->userinfo["id"]]);
        $alist = $this->getAllAddress();
        foreach ($addrlist as $k => $v) {
            $addrlist[$k]['province_name'] = $alist[$v['province']];
            $addrlist[$k]['city_name'] = $alist[$v['city']];
            $addrlist[$k]['district_name'] = $alist[$v['district']];
        }
        // $rule = [
        //     'province' => function($v,$val,$alist){return $alist[$v];},
        //     'city' => function($v,$val,$alist){return $alist[$v];},
        //     'district' => function($v,$val,$alist){return $alist[$v];},
        // ];
        // $addrlist = com_format($addrlist,$rule, $alist);
        suc(['addrlist'=>$addrlist]);
    }

    //获取子地区名称
    public function getAllAddress(){
        $rs = $this->db->query('select id,name from com_address');
        $map = [];
        foreach ($rs as $key => $value) {
            $map[$value['id']] = $value['name'];
        }
        return $map;
    }

    //添加地址
    public function addSave(){
        $d = $this->post();
        $rule = [
            'addressname'=>'require',
            'addresstel'=>'require',
            'pid'=>'require',
            'cid'=>'require',
            'did'=>'require',
            'addressmx'=>'require',
        ];
        $msg = [
            'addressname.require' => '请输入姓名',
            'addresstel.require' => '请输入电话',
            'pid.require' => '请选择省',
            'cid.require' => '请选择市区',
            'did.require' => '请选择地区',
            'addressmx.require' => '请填写详细地址',
        ];
        check($rule,$msg,$d);
        $default = $this->post("is_default");
        $data = [
            "user_id" => $this->userinfo["id"],
            "address_name" => $d['addressname'],
            "province" => $d['pid'],
            "city" => $d['cid'],
            "district" => $d['did'],
            "address" => $d['addressmx'],
            "address_phone" => $d['addresstel'],
            'is_default' => $default,
            "create_time" => time(),
            "update_time" => time()
        ];
        if($default == 1){
            //修改其他默认
            $this->db->update("user_address",['is_default'=>0],["user_id=?",[$this->userinfo["id"]]]);
        }
        $re = $this->db->insert("user_address",$data);
        if($re){
            return suc('保存成功!');
        }else{
            err('保存错误!');
        }
    }

    //编辑地址详情
    public function details(){
        $id = $this->post("id");
        //所有省
        $plist = $this->db->query('select id,name from com_address where parent_id=?',[1]);
        $ainfo = $this->db->query('select * from user_address where id=? and user_id=?',[$id,$this->userinfo["id"]],1);
        $d = ['addrinfo'=>$ainfo,'provincelist'=>$plist];
        if($ainfo){
            $clist = $this->db->query('select id,name from com_address where parent_id=?',[$ainfo["province"]]);
            $d['citylist'] = $clist;
            $dlist = $this->db->query('select id,name from com_address where parent_id=?',[$ainfo["city"]]);
            $d['districtlist'] = $dlist;
        }
        suc($d);
    }

    //编辑地址
    public function editSave(){
        $d = $this->post();
        $rule = [
            'addressname'=>'require',
            'addresstel'=>'require',
            'pid'=>'require',
            'cid'=>'require',
            'did'=>'require',
            'addressmx'=>'require',
        ];
        $msg = [
            'addressname.require' => '请输入姓名',
            'addresstel.require' => '请输入电话',
            'pid.require' => '请选择省',
            'cid.require' => '请选择市区',
            'did.require' => '请选择地区',
            'addressmx.require' => '请填写详细地址',
        ];
        check($rule,$msg,$d);
        $default = $this->post("is_default");
        $data = [
            "user_id" => $this->userinfo["id"],
            "address_name" => $d['addressname'],
            "province" => $d['pid'],
            "city" => $d['cid'],
            "district" => $d['did'],
            "address" => $d['addressmx'],
            "address_phone" => $d['addresstel'],
            'is_default' => (int)$default,
            "create_time" => time(),
            "update_time" => time()
        ];
        if($default == 1){
            //修改其他默认
            $this->db->update("user_address",['is_default'=>0],["user_id=?",[$this->userinfo["id"]]]);
        }
        $rs = $this->db->update("user_address",$data,["id=?",[$d['addressid']]]);
        if($rs){
            return suc('保存成功!');
        }else{
            err('保存错误!');
        }
    }

    //删除地址
    public function del(){
        $id = $this->post("id");
        $info = $this->db->query('select * from user_address where id=? and user_id=?',[$id,$this->userinfo["id"]]);
        if($info){
            $re = $this->db->delete('user_address',$id,'id');
            if($re){
                return suc('删除成功!');
            }else{
                err('删除失败!');
            }
        }else{
            err('删除失败!');
        }
    }

    //设置默认
    public function setDefault(){
        $id = $this->post("id");
        $info = $this->db->query('select * from user_address where id=? and user_id=?',[$id,$this->userinfo["id"]]);
        if($info){
            //修改其他默认
            $this->db->update("user_address",['is_default'=>0],["user_id=?",[$this->userinfo["id"]]]);
            $data = [
                'is_default' => 1
            ];
            $rs = $this->db->update("user_address",$data,["id=?",[$id]]);
            if($rs){
                suc('设置成功!');
            }else{
                err('设置失败!');
            }
        }else{
            err('设置失败!');
        }
    }

    //获取用户默认地址
    public function getDefault(){
        //用户地址信息获取
        $map = [];
        $map[] = ["and","user_id","=",$this->userinfo["id"]];
        $w = where($map);
        $address_info = $this->db->query("select * from user_address  $w[0] order by is_default desc",$w[1],1);
        $alist = $this->getAllAddress();
        if($address_info){
            $address_info['address'] = $alist[$address_info['province']] . " " . $alist[$address_info['city']] . " " . $alist[$address_info['district']] . " " . $address_info['address'];
        }
        $d = [
            'addr' => $address_info
        ];
        suc($d);
    }

}
?>
